<?php
namespace Admin\Controller;
use Tools\AdminController,    Model\AuthModel,
    Model\RoleModel;

class AuthController extends AdminController {
    
    function showlist(){
        $atmd = new AuthModel();
        
        //所有权限
        $flist = $atmd -> where('auth_level=0') -> select();
        $zlist = $atmd -> where('auth_level=1') -> select();
        
        $this -> assign('flist',$flist);
        $this -> assign('zlist',$zlist);
        $this -> display();
    }
     

    function add(){ 
        $atmd = new AuthModel();
        
        //所有父级权限
        $fauth = $atmd -> where('auth_level=0') -> select();
        
        //收集添加信息
        if(!empty($_POST)){
            $shu = $atmd -> create();
            if($shu){
                $shu = $atmd -> tianjia($shu);
                if($shu){
                    $this -> redirect('showlist');
                }
            }else{
                $error = $atmd -> getError();
                $this  -> alert($error);
            }
        }
        
        $this -> assign('fauth',$fauth);
        $this -> display();
    }
    
    
    function delete($auth_id){
        if(is_array($auth_id)){
             $str = implode(',', $auth_id);
        }else{
             $str = $auth_id;
        } 
        $atmd = new AuthModel();
       if($atmd->delete($str)){
           $this -> redirect('showlist');
       }else{
           $this -> redirect('showlist','',3,'删除失败');
       }               
    }
    
    
    function xiugai($auth_id){
        $atmd = new AuthModel();
        
        //所有父级权限
        $fauth = $atmd -> where('auth_level=0') -> select();
        
        //修改的权限信息
        $list = $atmd -> find($auth_id);
        
        //收集，更新
        if(!empty($_POST)){
            $shu = $atmd -> create();
            if($shu){
                if($atmd->save($shu)){
                    $this -> redirect('showlist');
                }else{
                    $this -> redirect('showlist');
                }
            }else{
                $error = $atmd -> getError();
                $this  -> alert($error);
            }
        }
                
        $this -> assign('fauth',$fauth);
        $this -> assign('list',$list);
        $this -> display();
    }
    
}